// Copyright (C) 2018 Peter Wong. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: pending
description: Behavior with a custom RegExp exec
info: |
  %RegExpStringIteratorPrototype%.next ( )
    [...]
    9. Let match be ? RegExpExec(R, S).

  Runtime Semantics: RegExpExec ( R, S )
    1. Assert: Type(R) is Object.
    2. Assert: Type(S) is String.
    3. Let exec be ? Get(R, "exec").
    4. If IsCallable(exec) is true, then
      a. Let result be ? Call(exec, R, « S »).
      b. If Type(result) is neither Object or Null, throw a TypeError exception.
      c. Return result.
features: [Symbol.matchAll]
---*/

function Test() {
    var regexp = /./g;
    var str = 'abc';
    var iter = regexp[Symbol.matchAll](str);

    var callArgs, callCount;
    function callNextWithExecReturnValue(returnValue) {
      callArgs = undefined;
      callCount = 0;

      RegExp.prototype.exec = function() {
        callArgs = arguments;
        callCount++;
        return returnValue;
      }

      return iter.next();
    }

    var firstExecReturnValue = ['ab'];
    var result = callNextWithExecReturnValue(firstExecReturnValue);
    assert.sameValue(result.value, firstExecReturnValue);
    assert(!result.done);

    assert.sameValue(callArgs.length, 1);
    assert.sameValue(callArgs[0], str);
    assert.sameValue(callCount, 1);

    result = callNextWithExecReturnValue(null);
    assert.sameValue(result.value, undefined);
    assert(result.done);

    assert.sameValue(callArgs.length, 1);
    assert.sameValue(callArgs[0], str);
    assert.sameValue(callCount, 1);
}

// preheat
const NUMBER_OF_PREHEAT = 1;
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}

ArkTools.jitCompileAsync(Test);
assert.sameValue(ArkTools.waitJitCompileFinish(Test), true, '[JIT] JIT disabled.');

// test
const NUMBER_OF_TEST = 1; // or 2
for (let i = 0; i < NUMBER_OF_PREHEAT; i++) {
    Test();
}
